(ns fgj-project.core
  (:require [clojure.pprint :refer :all]
            [merpg.2D.core :refer :all]
            [seesaw.core :refer [alert]]
            [merpg.2D.make-game :refer [make-game]]
            [merpg.2D.animation :refer :all]))

(alter-var-root #'*out* (constantly *out*))

(comment {:left "/Users/feuer2/Dropbox/FGJ 2014/sprite.png" :down "/Users/feuer2/Dropbox/FGJ 2014/sprite.png"
                                                       :up "/Users/feuer2/Dropbox/FGJ 2014/sprite.png" :right "/Users/feuer2/Dropbox/FGJ 2014/sprite.png"})

(defn rectangle-collision? [obj1 obj2]
  (let [obj1 (if (merpg-object? obj1)
               (assoc obj1 :width (width obj1)
                      :height (height obj1))
               obj1)
        {x1 :x y1 :y width1 :width height1 :height} obj1
        obj2 (if (merpg-object? obj2)
               (assoc obj2 :width (width obj2)
                      :height (height obj2))
               obj2)
        {x2 :x y2 :y width2 :width height2 :height} obj2]
        
  (not
   (or
    (> x2 (+ x1 width1))
    (< (+ x2 width2) x1)
    (> y2 (+ y1 height1))
    (< (+ y2 height2) y1)))))
   
(comment  (object "/Users/feuer2/Dropbox/memapper/varsi-ylos.jpg"
                    :y (- h 300)))

(defn point-inside-rect? [[x y] rect]
  (let [rect (if (merpg-object? rect)
               (assoc rect :width (width rect)
                      :height (height rect))
               rect)
        {left :x top :y width1 :width height1 :height} rect
        right (+ left width1)
        bottom (+ top height1)]
    (and (> x left)
         (> y top)
         (< x right)
         (< y bottom))))
    
(comment :clicky-thing (if (and (not (nil? (:x @click-coords)))
                                (point-inside-rect? (->> @click-coords
                                                         (map #(nth % 1))
                                                         vec) clicky-thing))
                         (object (draw-to-surface (image 400 400)
                                                  (with-color "#00FF00"
                                                    (Rect 10 10 150 150 :fill? true)
                                                    (Oval 100 100 100 100))))
                         clicky-thing))
(defn character-movement-stopped?
  "Returns if the guy won't move anymore to right"
  [char]
  (>
   (:x char)
   (/ (width) 2)))

(defn map-end? [map-x map-w screen-w]
  (let [w-left-on-screen (+ map-x map-w)]
    (< w-left-on-screen screen-w)))
  

(def ^:dynamic *limit-y* true)
(def ^:dynamic *stop-y* false)

(def tree
  ["Hello"
   ["Ello" "GO AWAY"]
   "How hav you been?"
   ["Fine, thanks for asking"
    "Realy fine, thanks for asking, how about you?"]
   "I hav been realy fine!!"
   ["Good to hear, hav you heard about the alice incident!!"
    "Did you heard what happened to alice?"]
   "Not sure, does it include sheepskin gloves, oil and Whip?"
   ["What? No, she caught her husband cheating on her!!!"
    "Its not that thing, she caught her husband whit a another woman."]
   "O dear, Poor Alice i wonder how she will take this, like the sheep wasnt enought...."
   ["What i heard that she and her lover hided in closet when her husband came home... whit hes lover!!!"
    "Karma is a bitch i would say, she was hiding in closet whit HER lover when her husband came home whit hes secretary..."]
   "The tall blonde, whit hair extention? Shes hav been Alfreds secretary aswell!!! "
   ["I dont think she had that big rack."
    "She is quite attractive woman..."]
   "You think Alfred could hav done the same whit that slut secretary?..."
   ["Im sorry dear, i think alfred was the hiding in closet whit alice...."
    "I think i heared that Alfred was having fun whit Alice, when she busted her husband."]
   " I cant bealieve it... How do you know?"
   [" Gardener who was filming everything behind the window hav it all on tape."
    " I saw it whit my own eyes, when i was jogging near by"
    " Their old lady next door published it on paper."]
   "That cant be!!! whole world must know about this by now..."
   [". Im sorry dear all you can do now is to hire hitman to kill Alfred."
    " You should just forget it, its not like anyone else will take you beside Alfred."
    " You should buy a gun..."]
   "Alfred just left for business strip, hes just leaving me middle of this crisis!!."
   []
   "Oh my god!!"
   ["like I hav always said men are pigs..."
    "like I hav always said men are pigs..."
    "like I hav always said men are pigs..."]
   "*Call ends*"
   ["Press 1/2/3"]
   "Where did the kid go...?"])
   

;(defn do-interaction []
  (let [h 768
        w 1024
        success (promise)
        f (merpg.2D.make-game/make-game {:discussion-state tree}
                                  :pre-drawqueue
                                  (fn [{state :discussion-state}]
                                    (Rect 0 0 (width) (height) :fill? true)
                                    (with-color "#000000"
                                      (Draw (first state) [(/ (width) 2) (/ (height) 2)])
                                      (Rect 20 (- h 300) (- w 40) 180)
                                      (let [x 30]
                                        (loop [y (- h 290)
                                               possibilities (second state)
                                               index 1]
                                          (when (first possibilities)
                                            (Draw (str index ". " (first possibilities)) [x y])
                                            (recur (+ y 20) (rest possibilities) (inc index))))))
                                    (Thread/sleep 50))
                                  :update (fn [{state :discussion-state}]
                                            (if (some key-down? [:1 :2 :3])
                                              (if (empty? (rest (rest state)))
                                                (merpg.2D.core/close-window)
                                                {:discussion-state (rest (rest state))})
                                              {:discussion-state state}))
                                  
                                  :window-width w
                                  :window-height h)]
;    (while (.isVisible f) )
    true)
        

(let [w 1024
      *stop-y* true
      h 768
      click-coords (atom {:x nil :y nil})
      child (animation {:left (Scale (image "/Users/feuer2/Dropbox/FGJ 2014/sprite2.png") 0.5 0.5) :down (Scale (image "/Users/feuer2/Dropbox/FGJ 2014/sprite2.png") 0.5 0.5)
                        :up (Scale (image "/Users/feuer2/Dropbox/FGJ 2014/sprite2.png") 0.5 0.5) :right (Scale (image "/Users/feuer2/Dropbox/FGJ 2014/sprite2.png") 0.5 0.5)}
                       :x 3)
      child (assoc child :y (- h (height child) 70))

      levels [{:child (image  "/Users/feuer2/Dropbox/FGJ 2014/Landscape/pngs/BG_lvl1_kid.png")
               :adult (image "/Users/feuer2/Dropbox/FGJ 2014/Landscape/pngs/BG_lvl1_adult.png" )} ;;Lvl1

              {:child (image  "/Users/feuer2/Dropbox/FGJ 2014/Landscape/pngs/BG_lvl2_kid.png" )
               :adult (image "/Users/feuer2/Dropbox/FGJ 2014/Landscape/pngs/BG_lvl2_adult.png" )} ;;Lvl2

              {:child (image  "/Users/feuer2/Dropbox/FGJ 2014/Landscape/pngs/BG_lvl1_kid.png" )
               :adult (image "/Users/feuer2/Dropbox/FGJ 2014/Landscape/pngs/BG_lvl1_adult.png" )}

              {:child (image  "/Users/feuer2/Dropbox/FGJ 2014/Landscape/pngs/BG_lvl4_kid.png")
               :adult (image "/Users/feuer2/Dropbox/FGJ 2014/Landscape/pngs/BG_lvl4_kid.png")}]

      level-index (atom 0)
      level-view (atom :child :validator (fn [new-val]
                                (in? [:child :adult] new-val)))
      map-x-location (atom 0)
      faces {:adult (Scale (image "/Users/feuer2/Dropbox/FGJ 2014/halo1.png") 0.5 0.5)
             :child (Scale (image "/Users/feuer2/Dropbox/FGJ 2014/halo 2.png") 0.5 0.5)}
      get-map-img (fn [index]
                    (let [level-view @level-view
                          img (-> (nth levels index)
                                  level-view)]
                      (Draw img [@map-x-location 0])))]
  (merpg.2D.make-game/make-game {:level-view :child
                                 :child child
                                 :adult (let [img (Scale (image "/Users/feuer2/Dropbox/FGJ 2014/adult-sprite.png") 0.5 0.5)]
                                          (animation {:left img :down img
                                                    :up img :right img}
                                                   :y (- h (height img) 70)
                                                   :x (- (:x child) (width img))))
                                 
                                 :the-dogs (comment (vec (->> ((fn dog-gen []
                                                                 (lazy-seq
                                                                  (cons (object (draw-to-surface (image 100 100)
                                                                                                 (with-color "#0000FF"
                                                                                                   (Rect 0 0 100 100 :fill? true))
                                                                                                 (with-color "#000000"
                                                                                                   (Draw "Minä olen koira" [0 10])))
                                                                                :x (rand-int w)
                                                                                :y (rand-int h))
                                                                        (dog-gen)))))
                                                              (take 5))))}

                                :post-drawqueue (fn [{child :child}]
                                                  (let [img (@level-view faces)]
                                                    (Draw img [(- (width) (width img)) 0])))
                                
                                :pre-drawqueue (fn [_]
                                                 (let [img (get-map-img @level-index)]
                                                   (Draw img [@map-x-location 0])))
                                :window-width w
                                :window-height h
                                :title "FGJ-project"                                
                                :mouse-clicked (fn [[x y]]
                                                 (swap! click-coords #(assoc % :x x :y y)))
                                
                                :update (fn [statemap]
                                          (Thread/sleep 20)

                                          (when (key-down? :space)
                                            (reset! level-view (if (= @level-view :adult)
                                                                 :child
                                                                 :adult))
                                            (Thread/sleep 100))
                                          
                                          (if (map-end? @map-x-location (width (get-map-img @level-index)) (width))
                                            (let [{child :child
                                                 bc :background
                                                 adult-bg :adult-bg
                                                 adult :adult
                                                 the-dogs :the-dogs} statemap]
                                              (reset! map-x-location 0)
                                              (when (>=
                                                     (swap! level-index inc)
                                                     (count levels))
                                                (alert "YOU WON! RESTART THE GAME!")
                                                (reset! level-index 0))

                                              (when (= @level-index 2)
                                                ;;Display the phone
                                                
                                              
                                                
                                              {:child (assoc child  :y (- h (height child) 70) :x 0)
                                               :adult (assoc adult
                                                        :y (- h (height @(:img adult)) 70)
                                                        :x (- (:x child) (width @(:img adult))))})
                                               
                                          (let [{child :child
                                                 bc :background
                                                 adult-bg :adult-bg
                                                 adult :adult
                                                 the-dogs :the-dogs} statemap
                                        ;                                                 bumpables :bumpables
                                        ;                                                clicky-thing :clicky-thing} statemap
                                                 toret (assoc child :x
                                                              (if (key-down? :right)
                                                                (do
                                                                  ((:set-direction! child) :right)
                                                                  (+ (:x child) 40))                                                
                                                                (if (key-down? :left)
                                                                  (do
                                                                    ((:set-direction! child) :left)
                                                                    (- (:x child) 40))
                                                                  (:x child)))
                                                              :y
                                                              (if-not *stop-y*
                                                                (do
                                                                  (println "Not stopped y - " *stop-y*)
                                                                  (if (key-down? :up)
                                                                    (- (:y child) 10)
                                                                    (if (key-down? :down)
                                                                      (+ (:y child) 10)
                                                                      (:y child))))
                                                                (:y child)))]
                                            
                                            (assoc statemap :child
                                        ;(if-not (some (partial rectangle-collision? toret) bumpables)
                                                   (assoc toret :y (if (and *limit-y*
                                                                            (< ;;The limiting-thing... CLEAN UP!
                                                                             h
                                                                             (+ (:y toret) (height toret))))
                                                                     (:y child)
                                                                     (if (> (- (double (/ h 2))
                                                                               100)
                                                                            (:y toret))
                                                                       (:y child)
                                                                       (:y toret)))
                                                          :x
                                                          (if (character-movement-stopped? toret)
                                                            (do
                                                              (swap! map-x-location #(- % 40))
                                                              (/ (width) 2))
                                                            (:x toret)))
                                                   :the-dogs the-dogs
                                                   :adult (assoc adult :x
                                                                 (- (:x (:child statemap)) (width adult))))))))))
